%Import data to fieldtrip and create trials by eventType
%data  struct from readEDFplus
%eventType int indicating the type of event (could be 2 rest 3 left 4 right )
function [trials]=defineTrials(data,eventType)

trials.fsample=data.fsample;
trials.label=data.label;
e_indexes=find([data.events(:).type]==eventType);


timestep=1/trials.fsample;

nTrials=length(e_indexes);
trials.trial=cell(1,nTrials);
trials.time=cell(1,nTrials);
for i=1:nTrials
        j=e_indexes(i);
        beg_sample=data.events(j).latency;
        end_sample=beg_sample+data.events(j).duration-1;        
       
        trials.trial{i}=data.data(:,beg_sample:end_sample);
        trials.time{i}=beg_sample/trials.fsample:timestep:end_sample/trials.fsample;
end





